package com.ymatou.iapi.fenqile.testcase;

import java.math.BigDecimal;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.fenqile.parameter.QueryFqlMemberInfoBean;
import com.ymatou.iapi.fenqile.service.QueryFqlMemberInfoCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.database.model.FqlMember;
import com.ymttest.database.model.FqlMemberExample;
import com.ymttest.database.sqlwapper.PayGateWayWapper;
/**
*分期乐会员信息查询
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryFqlMemberInfo{
	private static QueryFqlMemberInfoBean queryfqlmemberinfoBean;
	private static QueryFqlMemberInfoCall queryfqlmemberinfoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("分期乐会员信息查询");
	}

	@Before
	public void caseUp() {
		queryfqlmemberinfoBean=new QueryFqlMemberInfoBean();
		queryfqlmemberinfoCall=new QueryFqlMemberInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryfqlmemberinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryFqlMemberInfo_001() {
		Logger.start(true,"分期乐会员信息查询成功-存量数据");
		try {
			FqlMemberExample example=new FqlMemberExample();
			example.createCriteria().andNewUserFlagEqualTo(2).andStateEqualTo(1);
			example.setOrderByClause("userid desc limit 1");
			List<FqlMember> dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			queryfqlmemberinfoBean.setUserId(dbs.get(0).getUserId());
			queryfqlmemberinfoCall.setData(queryfqlmemberinfoBean);
			queryfqlmemberinfoCall.callService();			
			Logger.verifyEquals("200", queryfqlmemberinfoCall.getString("code"), "验证code");
			JsonObject result = queryfqlmemberinfoCall.getJsonObject().get("data").getAsJsonObject();
			//新用户标记:-1：异常，0：未知，1：未授信新用户，2：已授信新用户，3：老用户  ------- 3是老用户其他是新用户
			Logger.verifyEquals(result.get("IsNewUser").getAsBoolean(), dbs.get(0).getNewUserFlag()==3?false:true, "验证IsNewUser");
			Logger.verifyEquals(new BigDecimal(result.get("TotalCredit").getAsString()).setScale(2), dbs.get(0).getTotalCredit().setScale(2), "验证TotalCredit");
			Logger.verifyEquals(new BigDecimal(result.get("AvailableCredit").getAsString()).setScale(2), dbs.get(0).getAvailableCredit().setScale(2), "验证AvailableCredit");
			Logger.verifyEquals(new BigDecimal(result.get("UnReplayAmount").getAsString()).setScale(2), dbs.get(0).getUnReplayAmount().setScale(2), "验证UnReplayAmount");
			Logger.verifyEquals(result.get("CheckIdentity").getAsBoolean(), dbs.get(0).getCheckIdentity()==1, "验证CheckIdentity");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryFqlMemberInfo_002() {
		Logger.start(true,"不输入userid失败");
		try {
			queryfqlmemberinfoCall.callService();			
			Logger.verifyEquals("201", queryfqlmemberinfoCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryFqlMemberInfo_003() {
		Logger.start(true,"输入不存在userid失败");
		try {
			queryfqlmemberinfoBean.setUserId(-1);
			queryfqlmemberinfoCall.setData(queryfqlmemberinfoBean);
			queryfqlmemberinfoCall.callService();			
			Logger.verifyEquals("201", queryfqlmemberinfoCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryFqlMemberInfo_004() {
		Logger.start(true,"分期乐会员信息查询成功-新用户");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			int uid  = urrc.getUserid();
			//获取openid
			String open_id=PayGateWayCallService.CodeFin(uid).get("MockResult-FqlPay-open_id").toString();
			Thread.sleep(1000);
			PayGateWayCallService.CreditFin(uid, open_id);
			queryfqlmemberinfoBean.setUserId(uid);
			queryfqlmemberinfoCall.setData(queryfqlmemberinfoBean);
			queryfqlmemberinfoCall.callService();			
			Logger.verifyEquals("200", queryfqlmemberinfoCall.getString("code"), "验证code");
			JsonObject result = queryfqlmemberinfoCall.getJsonObject().get("data").getAsJsonObject();
			FqlMemberExample example=new FqlMemberExample();
			example.createCriteria().andUserIdEqualTo(uid);
			example.setOrderByClause("userid desc limit 1");
			List<FqlMember> dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			//新用户标记:-1：异常，0：未知，1：未授信新用户，2：已授信新用户，3：老用户  ------- 3是老用户其他是新用户
			Logger.verifyEquals(result.get("IsNewUser").getAsBoolean(), dbs.get(0).getNewUserFlag()==3?false:true, "验证IsNewUser");
			Logger.verifyEquals(new BigDecimal(result.get("TotalCredit").getAsString()).setScale(2), dbs.get(0).getTotalCredit().setScale(2), "验证TotalCredit");
			Logger.verifyEquals(new BigDecimal(result.get("AvailableCredit").getAsString()).setScale(2), dbs.get(0).getAvailableCredit().setScale(2), "验证AvailableCredit");
			Logger.verifyEquals(new BigDecimal(result.get("UnReplayAmount").getAsString()).setScale(2), dbs.get(0).getUnReplayAmount().setScale(2), "验证UnReplayAmount");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryFqlMemberInfo_005() {
		Logger.start(true,"分期乐会员信息查询失败-未知用户不返回");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			int uid  = urrc.getUserid();
			queryfqlmemberinfoBean.setUserId(uid);
			queryfqlmemberinfoCall.setData(queryfqlmemberinfoBean);
			queryfqlmemberinfoCall.callService();			
			Logger.verifyEquals("200", queryfqlmemberinfoCall.getString("code"), "验证code");
			Logger.verifyEquals(true, queryfqlmemberinfoCall.getJsonObject().get("data").isJsonNull(), "不返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryFqlMemberInfo_006() {
		Logger.start(true,"分期乐会员信息查询失败-未激活用户不返回-nocode");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			int uid  = urrc.getUserid();
			PayGateWayCallService.RegFqlMember(uid);
			queryfqlmemberinfoBean.setUserId(uid);
			queryfqlmemberinfoCall.setData(queryfqlmemberinfoBean);
			queryfqlmemberinfoCall.callService();			
			Logger.verifyEquals("200", queryfqlmemberinfoCall.getString("code"), "验证code");
			Logger.verifyEquals(true, queryfqlmemberinfoCall.getJsonObject().get("data").isJsonNull(), "不返回");
//			FqlMemberExample example=new FqlMemberExample();
//			example.createCriteria().andUserIdEqualTo(uid);
//			example.setOrderByClause("userid desc limit 1");
//			List<FqlMember> dbs = new PayGateWayWapper().selectFqlMemberExample(example);
//			//额度为0 无意义
////			Logger.verifyEquals(result.get("IsNewUser").getAsBoolean(), dbs.get(0).getNewUserFlag()==3?false:true, "验证IsNewUser");
//			//未授信
//			Logger.verifyEquals(new BigDecimal(0).setScale(2), dbs.get(0).getTotalCredit().setScale(2), "验证TotalCredit");
//			Logger.verifyEquals(new BigDecimal(result.get("AvailableCredit").getAsString()).setScale(2), dbs.get(0).getAvailableCredit().setScale(2), "验证AvailableCredit");
//			Logger.verifyEquals(new BigDecimal(result.get("UnReplayAmount").getAsString()).setScale(2), dbs.get(0).getUnReplayAmount().setScale(2), "验证UnReplayAmount");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryFqlMemberInfo_007() {
		Logger.start(true,"分期乐会员信息查询成功-老用户");
		try {
			FqlMemberExample example=new FqlMemberExample();
			example.createCriteria().andNewUserFlagEqualTo(3);
			example.setOrderByClause("userid desc limit 1");
			List<FqlMember> dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			queryfqlmemberinfoBean.setUserId(dbs.get(0).getUserId());
			queryfqlmemberinfoCall.setData(queryfqlmemberinfoBean);
			queryfqlmemberinfoCall.callService();			
			Logger.verifyEquals("200", queryfqlmemberinfoCall.getString("code"), "验证code");
			JsonObject result = queryfqlmemberinfoCall.getJsonObject().get("data").getAsJsonObject();
			//新用户标记:-1：异常，0：未知，1：未授信新用户，2：已授信新用户，3：老用户  ------- 3是老用户其他是新用户
			Logger.verifyEquals(result.get("IsNewUser").getAsBoolean(), dbs.get(0).getNewUserFlag()==3?false:true, "验证IsNewUser");
			Logger.verifyEquals(new BigDecimal(result.get("TotalCredit").getAsString()).setScale(2), dbs.get(0).getTotalCredit().setScale(2), "验证TotalCredit");
			Logger.verifyEquals(new BigDecimal(result.get("AvailableCredit").getAsString()).setScale(2), dbs.get(0).getAvailableCredit().setScale(2), "验证AvailableCredit");
			Logger.verifyEquals(new BigDecimal(result.get("UnReplayAmount").getAsString()).setScale(2), dbs.get(0).getUnReplayAmount().setScale(2), "验证UnReplayAmount");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryFqlMemberInfo_008() {
		Logger.start(true,"分期乐会员信息查询成功-授信失败用户");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			int uid  = urrc.getUserid();
			//获取openid
			String open_id=PayGateWayCallService.CodeFin(uid).get("MockResult-FqlPay-open_id").toString();
			PayGateWayCallService.CreditFin(uid, open_id,false);
			FqlMemberExample example=new FqlMemberExample();
			example.createCriteria().andUserIdEqualTo(uid);
			example.setOrderByClause("userid desc limit 1");
			List<FqlMember> dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			queryfqlmemberinfoBean.setUserId(dbs.get(0).getUserId());
			queryfqlmemberinfoCall.setData(queryfqlmemberinfoBean);
			queryfqlmemberinfoCall.callService();			
			Logger.verifyEquals("200", queryfqlmemberinfoCall.getString("code"), "验证code");
			JsonObject result = queryfqlmemberinfoCall.getJsonObject().get("data").getAsJsonObject();
			//新用户标记:-1：异常，0：未知，1：未授信新用户，2：已授信新用户，3：老用户  ------- 3是老用户其他是新用户
//			Logger.verifyEquals(result.get("IsNewUser").getAsBoolean(), dbs.get(0).getNewUserFlag()==3?false:true, "验证IsNewUser");
			Logger.verifyEquals(new BigDecimal(0).setScale(2), dbs.get(0).getTotalCredit().setScale(2), "验证TotalCredit");
			Logger.verifyEquals(new BigDecimal(result.get("AvailableCredit").getAsString()).setScale(2), dbs.get(0).getAvailableCredit().setScale(2), "验证AvailableCredit");
			Logger.verifyEquals(new BigDecimal(result.get("UnReplayAmount").getAsString()).setScale(2), dbs.get(0).getUnReplayAmount().setScale(2), "验证UnReplayAmount");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryFqlMemberInfo_009() {
		Logger.start(true,"分期乐会员信息查询成功-新用户-先查再授信等待5秒后才有结果");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			int uid  = urrc.getUserid();
			
			queryfqlmemberinfoBean.setUserId(uid);
			queryfqlmemberinfoCall.setData(queryfqlmemberinfoBean);
			queryfqlmemberinfoCall.callService();			
			Logger.verifyEquals("200", queryfqlmemberinfoCall.getString("code"), "验证code");
			
			//获取openid
			String open_id=PayGateWayCallService.CodeFin(uid).get("MockResult-FqlPay-open_id").toString();
			Thread.sleep(1000);
			PayGateWayCallService.CreditFin(uid, open_id);
			queryfqlmemberinfoBean.setUserId(uid);
			queryfqlmemberinfoCall.setData(queryfqlmemberinfoBean);
			queryfqlmemberinfoCall.callService();			
			Logger.verifyEquals("200", queryfqlmemberinfoCall.getString("code"), "验证code");
			Logger.verifyEquals(true,queryfqlmemberinfoCall.getJsonObject().get("data").isJsonNull(), "验证Result为空-此时还没有进缓存");
			Thread.sleep(6000);
			queryfqlmemberinfoCall.callService();	
			JsonObject result = queryfqlmemberinfoCall.getJsonObject().get("data").getAsJsonObject();
			FqlMemberExample example=new FqlMemberExample();
			example.createCriteria().andUserIdEqualTo(uid);
			example.setOrderByClause("userid desc limit 1");
			List<FqlMember> dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			//新用户标记:-1：异常，0：未知，1：未授信新用户，2：已授信新用户，3：老用户  ------- 3是老用户其他是新用户
			Logger.debug(FormatUtil.beanToGSONString(dbs));
			Logger.verifyEquals(result.get("IsNewUser").getAsBoolean(), dbs.get(0).getNewUserFlag()==3?false:true, "验证IsNewUser");
			Logger.verifyEquals(new BigDecimal(result.get("TotalCredit").getAsString()).setScale(2), dbs.get(0).getTotalCredit().setScale(2), "验证TotalCredit");
			Logger.verifyEquals(new BigDecimal(result.get("AvailableCredit").getAsString()).setScale(2), dbs.get(0).getAvailableCredit().setScale(2), "验证AvailableCredit");
			Logger.verifyEquals(new BigDecimal(result.get("UnReplayAmount").getAsString()).setScale(2), dbs.get(0).getUnReplayAmount().setScale(2), "验证UnReplayAmount");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}